'''
https://leetcode.cn/problems/flatten-binary-tree-to-linked-list/
'''
from typing import Optional


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
class Solution:

    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
        if not root: return False
        def f(root, cur):
            if not root.left and not root.right:
                return root.val + cur == targetSum
            cur += root.val
            res = False
            res = res or (f(root.left, cur) if root.left else False)
            res = res or (f(root.right, cur) if root.right else False)
            return res
        return f(root, 0)